package com.ltb.speedlottery.utils;

import android.util.Log;

import com.ltb.speedlottery.bean.GoodLuckLotteryData;

import java.util.Arrays;
import java.util.List;

/**
 * 类描述：
 * <p>
 * 作者：TB.Liu on 18-10-9 10:34
 */

public class Computer {

    public static int[] dataAnalysis(int[] array){
        //存储计算结果
        int[] resultArray = new int[2];
        Arrays.sort(array);
        if(array[0]==array[1] && array[1]==array[2]){
            resultArray[0] = 2;//豹子
        } else {
            if(array[0]==array[1] || array[1]==array[2]){
                resultArray[0] = 1;//对子
            } else {
                if((Math.abs(array[0]-array[1])==1 && Math.abs(array[1]-array[2])==1)//顺子
                        || (array[0] ==0 && array[1] == 1 && array[2] == 9)//特殊顺子　019
                        ||((array[0] ==0 && array[1] == 8 && array[2] == 9))){//特殊顺子　089
                    resultArray[1] = 2;
                } else {
                    if(Math.abs(array[0]-array[1])==1//半顺子
                            || Math.abs(array[1]-array[2])==1//半顺子
                            || Math.abs(array[0]-array[2])==1//半顺子
                            || (array[0] ==0 && array[2] == 9)){//特殊半顺子0＊9
                        resultArray[1] = 1;
                    }

                }
            }
        }
        return resultArray;
    }

    public static int dataAnalysisCouple(int[] array){
        Arrays.sort(array);
        int coupleCount = 0;
        for(int index = 0; index < 4; index++){
            if(array[index]==array[index+1]){
                coupleCount++;
            }
            Log.d("Tianbing", " array[index]="+array[index] + " : "+array[index+1] + " coupleCount= "+coupleCount );
        }
        return coupleCount;
    }

    public static int analysisAllData(List<GoodLuckLotteryData.ItemInfo> itemInfos, String targetDatas){
        if(itemInfos == null || itemInfos.size()<=0){
            return 0;
        }
        int count = 0;
        int size = itemInfos.size();
        GoodLuckLotteryData.ItemInfo itemInfo = null;
        for(int index = size -1 ; index >=0; index-- ){
            itemInfo = itemInfos.get(index);
            if("已开奖".equals(itemInfo.status)){
                if(targetDatas.contains(itemInfo.code.substring(2, 3))
                        && targetDatas.contains(itemInfo.code.substring(3, 4))
                        && targetDatas.contains(itemInfo.code.substring(4, 5))
                        && !isDistinct(itemInfo.code)){
                    if(count < 0){
                        count = 0;
                    }
                    count++;
                    Log.d("Tianbing", targetDatas + " contains "+itemInfo.code.substring(2, itemInfo.code.length()) +" is true");
                } else {
                    if(count > 0 ){
                        count = 0;
                    }
                    count--;
                    Log.d("Tianbing", targetDatas + " contains "+itemInfo.code.substring(2, itemInfo.code.length()) +" is false");
                }

            }
        }
        return count;
    }

    public static boolean analysisItemData(String code, String targetDatas){
        return (targetDatas.contains(code.substring(2, 3))
                && targetDatas.contains(code.substring(3, 4))
                && targetDatas.contains(code.substring(4, 5))
                && !isDistinct(code));
    }

    private static boolean isDistinct(String code){
        return (code.substring(2, 3).equals(code.substring(3, 4))
                || code.substring(2, 3).equals(code.substring(4, 5))
                || code.substring(3, 4).equals(code.substring(4, 5)));
    }
}
